<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>闭包实现命令模式</title>
</head>
<body>

    <button id="execute">Execute</button>
    <button id="undo">Undo</button>

    <script>
    
        var tv = {
            open: function (){
                console.log("Yes,it works");
            },
            close: function () {
                console.log("No,it fails");
            }
        };

        var OpenTvCommand = function (reciver) {
            this.reciver = reciver;
        };

        OpenTvCommand.prototype.execute = function () {
            this.reciver.open();
        };

        OpenTvCommand.prototype.undo = function () {
            this.reciver.close();
        };

        var setCommand = function (command) {
            document.getElementById("execute").onclick = function () {
                command.execute();
            },
            document.getElementById("undo").onclick = function () {
                command.undo();
            }
        };

        setCommand(new OpenTvCommand(tv));
    
    </script>


</body>
</html>